﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    public class TrainSuspendDAL
    {
        public static void ADD(List<TrainSuspendModel> suspends)
        {
            foreach (var item in suspends)
            {
                string sql = @"INSERT INTO `trainsuspend`
                            (`TrainNum`,
                            `LogicalSection`,
                            `SuspendCount`,
                            `ExamResultsTestQuestionID`,
                            `TrainNumber`,`SuspendTime`,`StationName`)
                            VALUES
                            (@TrainNum,
                            @LogicalSection,
                            @SuspendCount,
                            @ExamResultsTestQuestionID,
                            @TrainNumber,@SuspendTime,@StationName)";

                MySqlParameter[] parms = {
                new MySqlParameter("@TrainNum", item.TrainNum ),
                new MySqlParameter("@LogicalSection", item.LogicalSectionName),
                new MySqlParameter("@SuspendCount", item.SuspendCount ),
                new MySqlParameter("@ExamResultsTestQuestionID", item.ExamResultsTestQuestionID),
                new MySqlParameter("@TrainNumber", item.TrainNumber),
                new MySqlParameter("@SuspendTime", item.SuspendTime),
                new MySqlParameter("@StationName", item.StationName)
            };
                DbHelper.ExecuteNonQuery(sql, parms);
            }
        }

        public static List<TrainSuspendModel> GetTrainSuspendList(int ExamResultsTestQuestionID)
        {
            //string sql = "SELECT a.*,b.devname FROM TrainSuspend a inner join device b on a.LogicalSection=b.deviceid and b.devtype=9 where ExamResultsTestQuestionID = " + ExamResultsTestQuestionID;
            string sql = "SELECT * FROM TrainSuspend  where ExamResultsTestQuestionID = " + ExamResultsTestQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<TrainSuspendModel> list = new List<TrainSuspendModel>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetTrainLateModel(dt.Rows[i]));
                }
            }
            return list;
        }

        internal static TrainSuspendModel GetTrainLateModel(DataRow dr)
        {
            TrainSuspendModel info = new TrainSuspendModel();

            info.TrainNum = Convert.ToInt32(dr["TrainNum"]);
            info.LogicalSection =(dr["LogicalSection"]).ToString();
            info.LogicalSectionName = (dr["LogicalSection"]).ToString();
            info.SuspendCount = int.Parse(dr["SuspendCount"].ToString());
            info.ExamResultsTestQuestionID = int.Parse(dr["ExamResultsTestQuestionID"].ToString());
            info.TrainNumber = int.Parse(dr["TrainNumber"].ToString());
            info.SuspendTime = int.Parse(dr["SuspendTime"].ToString());
            info.StationName = (dr["StationName"].ToString());
            return info;
        }
    }
}
